Ext.define('TS.view.inv.ProductInventoryDialog', {
    extend: 'Ext.window.Window',
    alias : 'widget.productinventorydialog',
	mixins: {
        observable: 'Ext.util.Observable'
    },
    constructor: function () {
    	this.callParent();
        this.addEvents(
        {'selected':true}
        );
    },
    title : '存货商品选择',
    width:800,
    layout:'fit',
    height:400,
    config: {
    	prodId:null,
    	isOneShow:true,
    	val:null,
    	invAddId:null
    },
    modal:true,
    initComponent: function() {
    	Ext.apply(this, {
    		tbar :[
				{
					xtype: 'triggerfield',
					labelWidth :50,
					fieldLabel:'查询条件:',
					width:400,
					margin:'0,0,10,10',
	    	    	emptyText:'请输入商品编码或名称',
	    	    	trigger1Cls:Ext.baseCSSPrefix + "form-search-trigger",
	    	    	onTriggerClick:function(e){
	    	    		e.keyCode=e.ENTER;//设置为Enter事件
	    	    		this.fireEvent('specialkey',this,e);//点客户查询图标执行搜索
	    	    	},
	    	    	listeners: {
						specialkey:{
							fn:function(field,e){
								//回车搜索商品
								if(e.keyCode==e.ENTER){
									var txt=field.value;
									var grid=field.up('window').down('inventorysimplelist');
									if(!Ext.isEmpty(txt)){
										grid.store.on("beforeload",function(store){
									        Ext.apply(store.proxy,{url:'inv/invsearch.do'});
									        Ext.apply(store.proxy.extraParams, {val:txt,invAddId:field.up('window').getInvAddId()});
									    });
										grid.store.loadPage(1,{
											url:'inv/invsearch.do',
								     		params:{val:txt,invAddId:field.up('window').getInvAddId()},
								    		callback:function(records, operation, success){
										    	//
								    		},
									   		scope: this
										});
									}
								}
							}
						}
					}
				}
    		],
    		items:[{
    		    xtype: 'panel',
                layout: 'border',
                items:[
	        		Ext.create('TS.view.inv.InventorySimpleList',{
	        			autoScroll: true,
	        			title:'',
	        			closable: false,
	        			region:'center',
	        			listeners: {
	        				//双击
							itemdblclick:{
								fn:function(grid, record, item, index, e, eOpts){
									var records=[];
									records.push(record);
									var win=this.up('window');
									win.fireEvent('selected',records);
		                			win.hide();
								}
							},
							//回车
							cellkeydown:{
								fn:function(grid, td, cellIndex, record, tr, rowIndex, e, eOpts ){
									if(e.keyCode==e.ENTER){
										var records=[];
										records.push(record);
										var win=this.up('window');
										win.fireEvent('selected',records);
			                			win.hide();
									}
								}
							}
						}        			
	        		})
    			]
    		}],
        	buttons:[
	        	{
	                text: '确认',
	                scope: this,
	                handler:function(){
	                	var grid=this.down('inventorysimplelist');//this.items.items[0].items.items[0];
	                	var records=grid.getSelectionModel().getSelection();
	                	//确认没有选择行则不执行.
	                	if(records.length>0){
	                		this.fireEvent('selected',records);
	                		this.hide();
	                	}
	                	
	                }
	            },
	            {
	                text: '取消',
	                scope: this,
	                handler: this.close
	            }
	        ]
    	});

        this.callParent(arguments);
    },
	listeners: {
		render:{
			fn:function(){
				var grid=this.down('inventorysimplelist');
				var val=this.getVal();
				var invAddId=this.getInvAddId();
				if(!Ext.isEmpty(val)){
					grid.store.load({
						params:{val:val,invAddId:invAddId},
			    		callback:function(records, operation, success){
			    			if(records){
			    				//如果只有一条记录并且只有IsOneShow为false.
				    			if(records.length==1 && !this.getIsOneShow()){
				    				this.fireEvent('selected',records);
			                		this.hide();
				    			}
				    			if(records.length>0){
				    				//多条记录默认选择第一条
				    				grid.getSelectionModel().select(records[0],true);
									
				    			}
			    			}
			    		},
				   		scope: this
					});					
				}else{
					grid.store.removeAll();
				}
			}
		},
		show:{
			fn:function(){
				var grid=this.down('inventorysimplelist');
				if(grid.store.getTotalCount()>0){
					grid.focus(100);
				}else{
					var txtfield=this.down('triggerfield');
					txtfield.focus('',100);
				}
				
			}
		}
	}
});